<template>
  <div class="ele-body">
    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="dlmpId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          v-model:selection="selection"
          :scroll="{ x: 'max-content' }"
        >
          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openEdit(record)">修改</a>
                <a-divider type="vertical"/>
                <a-popconfirm title="确定要解绑此记录吗？" @confirm="remove(record)">
                  <a class="ele-text-danger">解绑</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <device-luoxun-milker-position-edit v-if="showEdit" v-model:visible="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script>
import {DeviceLuoxunMilkerPositionApi} from '@/api/property/milker/DeviceLuoxunMilkerPositionApi';
import DeviceLuoxunMilkerPositionEdit from './milker-position-edit.vue'
import { message } from 'ant-design-vue';
export default {
  name: 'DeviceManagerdeviceMilkerPosition',
  components: {
    DeviceLuoxunMilkerPositionEdit
  },
  props: {
    data: Object
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '位置',
          dataIndex: 'dlmPosition'
        },
        {
          title: '设备id',
          dataIndex: 'recordId'
        },
        {
          title: '设备号',
          dataIndex: 'recordNo'
        },
        {
          title: '终端唯一码',
          dataIndex: 'clientNum'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {"milkerId" : this.data.dlmId},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2023/02/05 13:55
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({page: 1});
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2023/02/05 13:55
     */
    reset() {
      this.where.milkerId = this.data.dlmId;
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2023/02/05 13:55
     */
    async remove(row) {
      const result = await DeviceLuoxunMilkerPositionApi.delete({dlmpId: row.dlmpId});
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2023/02/05 13:55
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({page, limit, where, orders}) {
      return DeviceLuoxunMilkerPositionApi.findPositionsOfMilkerPage({
        ...where, ...orders,
        pageNo: page,
        pageSize: limit
      });
    },
  }
};
</script>
